package com.codemart.controller;

import com.codemart.common.R;
import com.codemart.entity.EntityRole;
import com.codemart.entity.RoleEntity;
import com.codemart.result.Result;
import com.codemart.service.SystemService;
import com.codemart.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/system")
public class SystemController {

    @Autowired
    SystemService systemService;

    @PostMapping("/roleList")
    public Result roleList(@RequestBody(required = false) Integer id){
        List<RoleVO> roleVO = systemService.getRoleList(id);
        return Result.success(roleVO);
    }

    @PostMapping("/addRole")
    public Result addRole(@RequestBody RoleEntity roleEntity){
        systemService.addRole(roleEntity.getUrl(),roleEntity.getMsg());
        return Result.success();
    }

    @PostMapping("projectList")
    public Result projectList(@RequestBody(required = false) String title){
        List<ProjectInfoVO> projectInfos = systemService.getProjectInfoByTitle(title);
        return Result.success(projectInfos);
    }

    @PostMapping("/userList")
    public Result userList(@RequestBody(required = false) String search){
        List<UserVO> userList = systemService.getUserList(search);
        return Result.success(userList);
    }

    @PostMapping("/userRole")
    public Result userRole(@RequestBody(required = false) String search){
        List<UserRoleVO> userRoleVOList =  systemService.getUserRole(search);
        return Result.success(userRoleVOList);
    }

    @PostMapping("/entityRole")
    public Result entityRole(@RequestBody(required = false) EntityRole entityRole){
        List ids = entityRole.getIds().stream().sorted().toList();
        List oldIds = entityRole.getOld_ids().stream().sorted().toList();
        if (!ids.equals(oldIds)) {
            Set<Integer> idsDifference = new HashSet<>(ids);//在ids不在oldids中
            idsDifference.removeAll(oldIds);

            Set<Integer> oldIdsDifference = new HashSet<>(oldIds);//在oldids不在ids中
            oldIdsDifference.removeAll(ids);

            if(oldIdsDifference.size() != 0){
                //删除
                systemService.removeRolePermission(oldIdsDifference,entityRole.getId());
            }
            if(idsDifference.size() != 0){
                List<Integer> toRemove = new ArrayList<>();
                //新增
                idsDifference.forEach(i -> {
                    List<RolePermissionVO> rolePermissionVOList = systemService.getRolePermission(i,entityRole.getId());
                    if(rolePermissionVOList.size() != 0){
                        if(rolePermissionVOList.get(0).getRole_id().equals(i)){
                            systemService.changeRolePermissionStatus(i,entityRole.getId());
                            toRemove.add(i);
                        }
                    }
                });
                idsDifference.removeAll(toRemove);
                if(idsDifference.size() != 0){
                    systemService.addRolePermission(idsDifference,entityRole.getId());
                }
            }
        }else{
            //修改
            systemService.changePermissionMessage(entityRole.getId(),entityRole.getMsg(),entityRole.getUrl());
        }
        return Result.success();
    }
}
